<?php

class Register_model extends CI_Model {
	
	function validate()
	{
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		
		$url = "http://localhost:8080/moms-diary/services/authenticate";
		
		$context = stream_context_create(array(
				'http' => array(
						'header'  => "Authorization: Basic " . base64_encode($username.":".$password)
				)
		));
		$data = file_get_contents($url, false, $context);
		
		if(isset($data)) {
			return json_decode($data,TRUE);
		}
	}
	
	function getFeeds(){
		
		$url = "http://localhost:8080/moms-diary/services/feeds/".$this->session->userdata('userid');
	
		$auth = $this->session->userdata('username').":".$this->session->userdata('password');
	
		$context = stream_context_create(array(
				'http' => array(
						'header'  => "Authorization: Basic " . base64_encode($auth)
				)
		));
		$json = file_get_contents($url, false, $context);
	/*	
		$json = file_get_contents(base_url().'test/ScrapBook_multi.json', FILE_USE_INCLUDE_PATH);
		
		if(isset($json)) {
			return json_decode($json,TRUE);
		}
		*/
	}
	
	function add_user(){

		$this->load->library('curl');
		$this->curl->create('http://localhost:8080/moms-diary/services/registration');
		//$this->curl->http_login($username, $password);
		$this->curl->post(array(
					"mail" => $this->input->post('inputEmail'),
					"firstName" => $this->input->post('firstName'), 
					"lastName" => $this->input->post('firstName'), 
					"invitationId" => ""
				));
		$this->curl->execute();
		
		$info = $this->curl->info;
		
		if($info['http_code']== 201){
			return true;		
		}
		
	}
}